package com.irdstudio.apicenter.console.controller.authenticate;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.irdstudio.apicenter.console.controller.MacmsBaseAction;
import com.irdstudio.apicenter.console.dm.AppWxMenuConfig;
import com.irdstudio.apicenter.console.service.authenticate.AppWxMenuConfigService;


@Controller
@RequestMapping(value = "/appWxMenuConfig")
public class AppWxMenuConfigAction extends MacmsBaseAction {
	@Autowired
	private AppWxMenuConfigService appWxMenuConfigService;

	// 插入跳转
	@RequestMapping(value = "jumpAddWxMenuConfig")
	public String jumpAddWxMenuConfig(HttpServletRequest request) {
	    String appid = request.getParameter("appid");
		request.setAttribute("appid", appid);
		return "auth/addWxMenuConfig";
	}

	// 插入保存
	@RequestMapping(value = "addWxMenuConfig")
	public void addWxMenuConfig(@ModelAttribute("appWxMenuConfig") AppWxMenuConfig appWxMenuConfig,
			HttpServletRequest request, HttpServletResponse response)
			throws SQLException, ClassNotFoundException {
		int flag = -1;
		//将转义字符转回来
		appWxMenuConfig.setWx_menu_url(appWxMenuConfig.getWx_menu_url().replaceAll("~", "&"));
		flag = appWxMenuConfigService.insertOne(appWxMenuConfig);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}
	}

	// 更新跳转
	@RequestMapping(value = "jumpUpdateWxMenuConfig")
	public String jumpUpdateWxMenuConfig(HttpServletRequest request)
			throws SQLException, ClassNotFoundException {
		String recordKey = request.getParameter("recordKey");
		String appid = request.getParameter("appid");
		AppWxMenuConfig appWxMenuConfig = appWxMenuConfigService.queryAppTradePrivate(recordKey);
		request.setAttribute("appWxMenuConfig", appWxMenuConfig);
		request.setAttribute("appid", appid);
		return "auth/updateWxMenuConfig";
	}

	// 更新保存
	@RequestMapping(value = "updateWxMenuConfig")
	public void updateWxMenuConfig(@ModelAttribute("appWxMenuConfig") AppWxMenuConfig appWxMenuConfig,
			HttpServletRequest request, HttpServletResponse response)
			throws SQLException, ClassNotFoundException {
		int flag = -1;
		//将转义字符转回来
		appWxMenuConfig.setWx_menu_url(appWxMenuConfig.getWx_menu_url().replaceAll("~", "&"));
		flag = appWxMenuConfigService.update(appWxMenuConfig);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}
	}

	// 删除
	@RequestMapping(value = "deleteWxMenuConfig")
	public void deleteWxMenuConfig(HttpServletRequest request,
			HttpServletResponse response) throws SQLException,
			ClassNotFoundException {
		int flag = -1;
		String recordKey = request.getParameter("recordKey");
		flag = appWxMenuConfigService.delete(recordKey);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}
	}

	// 查看
	@RequestMapping(value = "queryWxMenuConfigDetail")
	public String queryWxMenuConfigDetail(HttpServletRequest request) throws SQLException,
			ClassNotFoundException {
		String recordKey = request.getParameter("recordKey");
		String appid = request.getParameter("appid");
		AppWxMenuConfig appWxMenuConfig = appWxMenuConfigService.queryAppTradePrivate(recordKey);
		request.setAttribute("appWxMenuConfig", appWxMenuConfig);
		request.setAttribute("appid", appid);
		return "auth/queryWxMenuConfigDetail";
	}

}

